<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Lap_BOOM_omset.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
<h3>LAPORAN BOOM</h3>
<div class="clr"></div>

<?php
	$width_tabel='100%'; // untuk ukuran tabel bila belum pilih kriteria
	$width_tabel='100%'; // untuk ukuran tabel bila sudah pilih kriteria
?>

<?php

function get_tot_omzet($strkode_dealer, $unit, $jumLevel, $mulai, $tglstart, $tglend){
	$sql = "select member.intid_dealer, member.intid_unit, member.intlevel_dealer, member.strkode_upline, member.strkode_dealer, upper(member.strnama_dealer) strnama_dealer, (get_omzet(member.intid_dealer, member.intid_unit, '$tglstart', '$tglend'))omset, (member_hier_sum(member.strkode_dealer, member.intid_unit, '$tglstart', '$tglend')) omset_unit 
from member
where member.strkode_upline ='$strkode_dealer' and member.intid_unit = $unit 
group by member.intid_dealer, member.intid_unit, member.intlevel_dealer, member.strkode_upline, member.strkode_dealer, member.strnama_dealer
order by member.intlevel_dealer asc";
	
	$get = mysql_query($sql);
	
	if (mysql_num_rows($get)>0) {
		while($row = mysql_fetch_array($get)) {
			$level = $row['intlevel_dealer'];
											
					for($i=1; $i<=$jumLevel; $i++):					
						$name['level_'.$i] = '';
					endfor;
					
					$name['level_'.$level] = isset($row['strnama_dealer'])?$row['strnama_dealer']:'';
					if($row['omset'] != 0){
					echo "<tr>";
					
					echo "<td>".$row['strkode_dealer']."</td>";
					for($x=$mulai; $x<=$jumLevel; $x++):
						echo "<td>".$name['level_'.$x]."</td>";	
					endfor;
					echo "<td>".$row['omset']."</td>";
					echo "<td>".$row['omset_unit']."</td>";
					echo "</tr>";
					}
					get_tot_omzet($row['strkode_dealer'], $row['intid_unit'], $jumLevel, $mulai, $tglstart, $tglend);					
		}
	}
	
			
}

function get_unit ($induk, $unit, $jumLevel, $mulai, $tglstart, $tglend) {
		
		$get = mysql_query("select member.intid_dealer, member.intid_unit, member.intlevel_dealer, member.strkode_upline, member.strkode_dealer, 
upper(member.strnama_dealer) strnama_dealer,(get_omzet(member.intid_dealer, member.intid_unit, '$tglstart', '$tglend'))omset, (member_hier_sum(member.strkode_dealer, member.intid_unit, '$tglstart', '$tglend')) omset_unit  
from member
where member.strkode_dealer='$induk' and member.intid_unit = $unit
group by member.intid_dealer, member.intid_unit, member.intlevel_dealer, member.strkode_upline, member.strkode_dealer, member.strnama_dealer
order by member.intlevel_dealer asc");

		if (mysql_num_rows($get)>0) {
				while($row = mysql_fetch_array($get)) {
					$level = $row['intlevel_dealer'];
					for($i=1; $i<=$jumLevel; $i++):					
						$name['level_'.$i] = '';
					endfor;
					
					$name['level_'.$level] = isset($row['strnama_dealer'])?$row['strnama_dealer']:'';
					echo "<tr>";
					
					echo "<td>".$row['strkode_dealer']."</td>";
					for($x=$mulai; $x<=$jumLevel; $x++):
						echo "<td>".$name['level_'.$x]."</td>";	
					endfor;
					echo "<td>".$row['omset']."</td>";
					echo "<td>".$row['omset_unit']."</td>";
					echo "</tr>";
					get_tot_omzet($row['strkode_dealer'], $row['intid_unit'], $jumLevel, $mulai, $tglstart, $tglend);
					
										
				}
		}
		
		
					
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table border="1" width="100%">
	<thead>
		<th>Kode</th>
        <?php 
			for($x=$mulai; $x<=$jumLevel; $x++):
				echo "<th>Dealer ".$x."</td>";	
			endfor;	
		?>
		<th>Omset</th>
       	<th>Omset Unit</th>
	</thead>
	<tbody>
		<?php get_unit($parent, $unit, $jumLevel, $mulai, $tglstart, $tglend); ?>
	</tbody>
</table>
</body>
</html>
